package android.gpstrac.controls;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.gpstrac.R;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.widget.*;

/**
 * Used to control the client/server communication service.
 *
 * @author Nicolas Papin
 */
public class GPSTracControls extends Activity
{
	//
	// CLASS AND INSTANCE DATA
	//
	/**
	 * Saved settings
	 */
	public static final String PREFS = "GPSTracSettings";
	public static final String UNPREF = "username";
	public static final String SPREF = "isStarted";
	

	//
	// METHODS
	//
	@Override
	protected void onCreate(Bundle state)
	{
		super.onCreate(state);
		
		startService(new Intent(GPSTracUpdateService.class.getName()));
		
		setContentView(R.layout.settings);
		
		// Restore saved data.
		SharedPreferences settings = getSharedPreferences(PREFS, 0);
		String userName = settings.getString(UNPREF, "");
		boolean running = settings.getBoolean(SPREF, false);  // Check to see if the server is running currently.
		
		// Set up the username modification button.
		final Button locButton = (Button)findViewById(R.id.force_location_update);
		locButton.setEnabled(running);
		locButton.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
	    		sendBroadcast(new Intent(GPSTracUpdateService.class.getName() + ".sendLoc"));
			}
		});
		
		// Set up the username modification button.
		final Button unameButton = (Button)findViewById(R.id.username_set_button);
		unameButton.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
	    		updateUsername();
			}
		});
		
		// Set up the username text field.
		final EditText edittext = (EditText)findViewById(R.id.username_text);
		edittext.setText(userName);
		edittext.setOnKeyListener(new View.OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) 
			{
		    	// If the event is a key-down event on the "enter" button
		    	if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) 
		    	{
		    		updateUsername();
		    		return true;
		    	}
		    	
		    	return false;
		    }
		});
		
		final ToggleButton serviceControl = (ToggleButton)findViewById(R.id.service_startstop_toggle);
		serviceControl.setChecked(running);
		serviceControl.setOnClickListener(new View.OnClickListener()
		{		
			@Override
			public void onClick(View v)
			{
				boolean state = ((ToggleButton)v).isChecked();
				
				Editor edit = getSharedPreferences(PREFS, 0).edit();
				edit.putBoolean(SPREF, state);
				edit.commit();
				
				locButton.setEnabled(state);
				
				sendBroadcast(new Intent(GPSTracUpdateService.class.getName() + ".toggle"));
			}
		});
	}
	
	/**
	 * Used to update the query service's username.
	 */
	private void updateUsername()
	{
		final EditText edittext = (EditText)findViewById(R.id.username_text);
		Editor edit = getSharedPreferences(PREFS, 0).edit();
		edit.putString(UNPREF, edittext.getText().toString());
		edit.commit();
		
		sendBroadcast(new Intent(GPSTracUpdateService.class.getName() + ".username"));
	}
}
